$(function () {
    var folderForm = $("#folder_form");
    var folderDialog = $("#folder_dialog");
    var hanCloudTree = $("#hanCloud_tree");
    var fileForm = $("#file_form");
    var fileImport = $("#myfile");

    hanCloudTree.tree({
        url:'/hancloud/queryMenusByEmployeeId'
    });


    var cmdObj = {
        //新增一个文件夹
        'save':function () {
            //先获得当前被选中的节点
            var selectTree = hanCloudTree.tree("getSelected");
            var parentId = null;
            //获得父级Id
            if (selectTree) {
                parentId = selectTree.id;
            }


            folderForm.form('submit', {
                url:'/hancloud/save',
                onSubmit: function(param){
                    param['parentId']=parentId;
                },
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.alert("温馨提示","新建文件夹成功","info",function () {
                            //刷新树
                            hanCloudTree.tree("reload");
                            //关闭对话框
                            cmdObj.cancel();
                        });
                    }
                }
            });
        },

        //关闭新建文件夹对话框
        'cancel':function () {
            folderDialog.dialog("close");
        },
        //打开新增文件夹对话框
        'create_folder':function () {
            //先清除表单数据
            folderForm.form("clear");
            //后打开对话框
            folderDialog.dialog("open");
        },
        //文件上传
        'upload_file':function () {
            //先获得当前被选中的节点
            var selectTree = hanCloudTree.tree("getSelected");
            var parentId = null;
            var file = fileImport.filebox("files");
            console.log(file);
            var fileName = file[0].name;
            console.log(fileName);
            //获得父级Id
            if (selectTree && selectTree.url == null) {
                parentId = selectTree.id;
            }
            fileForm.form('submit', {
                url:'/hancloud/upload',
                onSubmit: function(param){
                    param['parentId']=parentId;
                    param['fileName']=fileName;
                },
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.alert("温馨提示","上传文件成功","info",function () {
                            //刷新树
                            hanCloudTree.tree("reload");
                        });
                    }
                }
            });
        },
        //文件下载
        'douwload_file':function () {
            //获得树选中的节点
            var selectTree = hanCloudTree.tree("getSelected");
            if (!selectTree) {
                $.messager.alert("提示","请选择一个想要下载的文件","error");
                return;
            }
            //选择的是文件夹 提示
            if (!selectTree.url) {
                $.messager.alert("提示","请选择一个想要下载的文件","error");
                return;
            }

            var url = selectTree.url + '&filename=' + selectTree.text;
            window.location.href=url;
        },

        //删除文件
        'delete_file':function () {
            //获得树选中的节点
            var selectTree = hanCloudTree.tree("getSelected");
            if (!selectTree) {
                $.messager.alert("提示","请选择一个想要删除的文件","error");
                return;
            }
            //选择的是文件夹 提示
            if (!selectTree.url) {
                $.messager.alert("提示","请选择一个想要删除的文件","error");
                return;
            }
            console.log(selectTree);
            //温馨提示
            $.messager.confirm("温馨提示","真的要删除文件吗?",function (yes) {
                if (yes) {
                    //发送ajax请求删除文件
                    $.post("/hancloud/delete",{"id":selectTree.id,"filePath":selectTree.url},function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","删除成功","info",function () {
                                //刷新树
                                hanCloudTree.tree("reload");
                            })
                        }
                    },'json');
                }
            })
        }
    };


    //统一给data-cmd绑定点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    });
})